package com.wenx.restcamel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONPath;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.ReadContext;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.DefaultMessage;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class AppHttp3 {
    public static void main(String[] args) throws Exception {
        CamelContext context = new DefaultCamelContext(); // 1. 创建 CamelContext.
        context.setTracing(true);
        context.start(); // 4. 启动CamelContext.
        context.addRoutes((new AppHttp3()).new test1());
        Thread.sleep(Integer.MAX_VALUE);  // 为了保持CamelContext处于工作状态，这里需要sleep主线程
        context.stop(); // 最后停止CamelContext
    }
    public class test1 extends RouteBuilder{
        @Override
        public void configure() throws Exception {
            from("timer:foo?repeatCount=1")
                    .to("http://localhost:8080/demo/hello6?id=123&name=abc")
                    //.setHeader("type",jsonPathExpression)
                    .process(new Processor() {
                        @Override
                        public void process(Exchange exchange) throws Exception {
                            //解析exchange中的返回值
                            DefaultMessage message = (DefaultMessage) exchange.getIn();
                            InputStream bodyStream =  (InputStream)message.getBody();
                            ByteArrayOutputStream result = new ByteArrayOutputStream();
                            byte[] buffer = new byte[1024];
                            int length;
                            while ((length = bodyStream.read(buffer)) != -1) {
                                result.write(buffer, 0, length);
                            }
                            //rest响应数据集
                            String bodystr = result.toString("UTF-8");
                            String data = "{\"data\":" + bodystr + "}";
                            result.close();
                            bodyStream.close();
                            //com.alibaba.fastjson.JSONObject jsonObject = JSON.parseObject(data);

                            /*//jsonpath过滤验证
                            //context.read("$.data[?((@.id > 500 && @.name == '王五') || @.id == 123)]")
                            System.out.println("过滤的值:"+JSONPath.eval(jsonObject,"$.data.*"));*/
                            ReadContext context = JsonPath.parse(data);
                            List<Map<String, Object>> mapList =
                                    context.read("$.data[?((@.id > 500 && @.name == '王五') || @.id == 123)]");
                            for(Map<String, Object> map : mapList){
                                for(Map.Entry<String,Object> eap : map.entrySet()){
                                    System.out.println("map的值: "+eap.getKey()+"  "+eap.getValue());
                                }
                            }
                            JSONArray arrayJson = JSONArray.fromObject(mapList);
                            //JSONObject objectJson = JSONObject.fromObject(result1);
                           exchange.getOut().setBody(arrayJson);
                        }
                    })
                    /*.filter()
                        .jsonpath("$.data[?((@.id > 500 && @.name == '王五') || @.id == 123)]")
                        .to("log:input").end()*/
                        .process(new Processor() {
                            @Override
                            public void process(Exchange exchange) throws Exception {
                                //JSONObject objectJson = JSONObject.fromObject(exchange.getIn().getBody());
                                /*JSONArray arrayJson = JSONArray.fromObject(exchange.getIn().getBody());
                                if (arrayJson.size()>0){
                                    for(int i=0;i<arrayJson.size();i++){
                                        Iterator iterator = arrayJson.getJSONObject(i).keys();
                                        //不知道key的情况，循环获取key value
                                        while (iterator.hasNext()){
                                            String key = iterator.next().toString();
                                            System.out.println("组装出来的值1= "+key+"  "+arrayJson.getJSONObject(i).get(key));
                                        }
                                    }
                                }*/
                                String body = exchange.getIn().getBody().toString();
                                System.out.println("body="+body);
                                ReadContext context = JsonPath.parse(body);
                                List<Map<String, Object>> mapList =
                                        context.read("$.[?(@.id > 500 && @.name == '王五')]");
                                for(Map<String, Object> map : mapList){
                                    for(Map.Entry<String,Object> eap : map.entrySet()){
                                        System.out.println("map的值2: "+eap.getKey()+"  "+eap.getValue());
                                    }
                                }

                            }
                        });
        }
    }
}
